<?php
/**
 * 用户
 * @author Administrator
 *
 */

function is_mobile($mobilep_no) {
	if(preg_match("/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/",$mobilep_no)){
		//验证通过
		return true;
	}else{
		//手机号码格式不对
		return false;
	}
}

class UserAction extends Action {
    public function login(){
    	$this->display();
    }
    
    public function doLogin() {
    	$mobile_no = $_POST['mobile_no'];
    	$password =  $_POST['password'];
    	$user = M('UserInfo')->where("mobile_no='{$mobile_no}' AND password='{$password}'")->find();
    	if (!$user) { //未注册
    		$this->error('登录失败，请重试！');
    	} else {
    		$_SESSION["username"] = $user["name"];
    		$_SESSION["mobile_no"] = $user["mobile_no"];
    		$_SESSION["user"] = $user;
    		redirect(U('/'));
    	}
    	
    }
    
    
    public function regstep1() {
    	if (IS_POST) { //注册用户
    		
    		if (!is_mobile($_POST['mobile_no'])) {
    			$this->error('请输入正确的手机号！');
    		}

    		if(M('UserInfo')->where("mobile_no='{$_POST['mobile_no']}'")->find()){
    			$this->error('手机号已被注册！');
    		}
    		
    		$ownerInfo = M('OwnerInfo')->where("mobile_no='{$_POST['mobile_no']}'")->find();
    		if(!$ownerInfo){
    			$this->error('业主信息不存在！');
    		}
    		
    		/* 检测昵称 */
    		if (!$_POST['name']) {
    			$this->error('请输入昵称！');
    		}
    		if(M('UserInfo')->where("name='{$_POST['name']}'")->find()){
    			$this->error('昵称已被注册！');
    		}
    		/* 检测密码 */
    		if (!$_POST['password']) {
    			$this->error('请输入密码！');
    		}
    		/* 检测密码 */
    		if ($_POST['password'] != $_POST['repassword']) {
    			$this->error('密码和重复密码不一致！');
    		}
    		$this->assign('mobile_no',$_POST['mobile_no']);
    		$this->assign('name',$_POST['name']);
    		$this->assign('password',$_POST['password']);
    		$this->assign('community_no',$ownerInfo->community_no);
    		$this->assign('building_no',$ownerInfo->building_no);
    		$this->assign('room_no',$ownerInfo->room_no);
    		$this->display('register2');
    	}
    }
    
    public function regstep2() {
    	if (IS_POST) { //注册用户
    		$UserInfo   =   D('UserInfo');
    		if($UserInfo->create()) {
    			$result =   $UserInfo->add();
    			if($result) {
    				$this->success('注册成功！');
    			}else{
    				$this->error('注册出错！');
    			}
    		}else{
    			$this->error($UserInfo->getError());
    		}
    	}
    }
}